/**
 * com.chaeron.GPS - An API for access to GPS devices.
 *
 *
 * Copyright (c) 2001, 2002 Chaeron Corporation,
 * All rights reserved.
 * 
 * Chaeron grants you a non-exclusive license to use, modify and re-distribute
 * this program under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version, provided that this copyright notice and 
 * license appear on all copies of the software.
 * 
 * Software is provided "AS IS," without a warranty of any kind. ALL EXPRESS OR
 * IMPLIED REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
 * HEREBY EXCLUDED. THE ENTIRE RISK ARISING OUT OF USING THE SOFTWARE IS ASSUMED
 * BY THE LICENSEE. See the GNU General Public License for more details.
 *
 * CHAERON AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
 * LICENSEE OR ANY THIRD PARTY AS A RESULT OF USING OR DISTRIBUTING SOFTWARE.
 * IN NO EVENT WILL CHAERON OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE,
 * PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL
 * OR PUNITIVE DAMAGES, HOWEVER CAUSED AN REGARDLESS OF THE THEORY OF LIABILITY,
 * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF CHAERON HAS
 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

 * @version     1.02
 * @author      Andrzej Jan Taramina, Chaeron Corporation
 *              andrzej@chaeron.com
 */

package com.chaeron.GPS.NMEA;

//***************************************************************************
//*
//*     Imports
//*
//***************************************************************************/

import com.chaeron.GPS.*;
import com.chaeron.GPS.event.*;
import com.chaeron.GPS.exception.*;


//***************************************************************************
//*
//*     GSASentence Class definition
//*
//***************************************************************************/

/**
 * The GSASentence class represents NMEA GSA - DOP & Active Satellites
 *
 */

public class GSASentence extends NMEASentence implements GPSSatellitesActive
{
	//***************************************************************************
	//*
	//*     Constant definitions
	//*
	//***************************************************************************/
	
	private final static int		GSA_NUM_FIELDS				= 17;
	private final static int		MAX_SATELLITES				= 12;
	
	
	//***************************************************************************
	//*
	//*     Attributes
	//*
	//***************************************************************************/
	
	private char		selectionMode				= ' ';
	private char		mode						= ' ';
	private short[]		satelliteID					= new short[ MAX_SATELLITES ];
	private float		pdop						= 0.0f;
	private float		hdop						= 0.0f;
	private float		vdop						= 0.0f;
	
	
	//***************************************************************************
	//*
	//*     Constructors
	//*
	//***************************************************************************/
	
	/**
	 * Create a GSASentence from the given packet data
	 *
	 * @param	tid			talker ID
	 * @param	sid			Sentence ID
	 * @param	sentenceStr	sentence string buffer
	 */
	
	GSASentence( int tid, int sid, String sentenceStr )
	{  
		super( tid, sid, sentenceStr );
		
		setFieldCount( GSA_NUM_FIELDS );
	}
	
	
	//***************************************************************************
	//*
	//*     Methods
	//*
	//***************************************************************************/
	
	/**
	 * Decode the data fields for a GSA sentence
	 *
	 * @return		This method will return a standard error code or GPS.ERROR_OK
	 * @throws GPSPacketException
	 * @throws GPSFunctionUnavailableException
	 */
	
	public void parseData() throws GPSPacketException, GPSFunctionUnavailableException
	{  
		int		i;
		String	field;
		
		if( getFieldCount() >= GSA_NUM_FIELDS ) {
			field 				= getField( 0 );
			if( field.length() > 0 ) {
				selectionMode		= field.charAt( 0 );				// Selection Mode
			}
			
			field 				= getField( 1 );
			if( field.length() > 0 ) {
				mode			= field.charAt( 0 );					// Mode
			}
			
			for( i = 0; i < MAX_SATELLITES; i++ ) {						// Satellite IDs
				satelliteID[ i ] = getNMEAShort( getField( i + 2 ) );
			}
			
			pdop				= getNMEAFloat( getField( 14 ) );		// PDOP
			hdop				= getNMEAFloat( getField( 15 ) );		// HDOP
			vdop				= getNMEAFloat( getField( 16 ) );		// VDOP
			
		} else {
			throw( new GPSPacketException( GPSPacketException.EXCEPTION_NUMFIELDS_INVALID ) );
		}
	}
	
	
	//***************************************************************************
	//*
	//*     Sentence Processing Methods
	//*
	//***************************************************************************/
	
	/**
	 * Process the sentence
	 *
	 * @param	gps			the GPS object that requested the GSA sentence to be processed
	 */
	
	void process( GPS gps )
	{
		gps.postGPSEvent( new GPSEvent( GPSEvent.GPS_EVENT_SATELLITES_ACTIVE, this ) );
	}
	
	
	/**
	 * Get events generated.
	 *
	 * @return		return a valid event type(s) that will be generated by this sentence subclass
	 */
	
	static int getEventsGenerated()
	{  
		return( GPSEvent.GPS_EVENT_SATELLITES_ACTIVE | GPSEvent.GPS_EVENT_NMEA_SENTENCE );
	}
	
	
	//***************************************************************************
	//*
	//*    Accessor methods
	//*
	//***************************************************************************/
	
	/**
	 * Get the Selection Mode
	 *
	 * @return		selection mode
	 */
	
	public char getSelectionMode()
	{  
		return( selectionMode );
	}
	
	/**
	 * Get the Mode
	 *
	 * @return	 mode
	 */
	
	public char getMode()
	{  
		return( mode );
	}
	
	
	/**
	 * Get number of satellites
	 *
	 * @return	 number of satellites
	 */
	
	public int getSatelliteCount()
	{  
		return( MAX_SATELLITES );
	}
	
	
	/**
	 * Get the specified satellite ID
	 *
	 * @param	index	index of satellite
	 * @return			satellite ID
	 */
	
	public short getSatelliteID( int index )
	{  
		short		id = -1;
		
		if( index >= 0 && index < MAX_SATELLITES ) {
			id = satelliteID[ index ];
		}
		
		return( id );
	}
	
	
	/**
	 * Get the PDOP (meters)
	 *
	 * @return		PDOP (meters)
	 */
	
	public float getPDOP()
	{  
		return( pdop );
	}
	
	
	/**
	 * Get the HDOP (meters)
	 *
	 * @return		HDOP (meters)
	 */
	
	public float getHDOP()
	{  
		return( hdop );
	}
	
	
	/**
	 * Get the VDOP (meters)
	 *
	 * @return		VDOP (meters)
	 */
	
	public float getVDOP()
	{  
		return( vdop );
	}
	
}
